﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QLPMT_DTO;
using QLPMT_BUS;

namespace my_project_qlpmt1
{
    public partial class DONTHUOCMOI : Form
    {
        #region Cac bien thanh phan
        private string _maphieukham;

        public string Maphieukham
        {
            get { return _maphieukham; }
            set { _maphieukham = value; }
        }
        private string _maloaibenh;

        public string Maloaibenh
        {
            get { return _maloaibenh; }
            set { _maloaibenh = value; }
        }
        private BindingList<ChiTietPhieuKham> listchitietphieukham = new BindingList<ChiTietPhieuKham>();

        public BindingList<ChiTietPhieuKham> Listchitietphieukham
        {
            get { return listchitietphieukham; }
            set { listchitietphieukham = value; }
        }

        #endregion
        // thong tin ve thuoc
        public void ThongTinThuoc()
        {
            List<Thuoc> list = new List<Thuoc>();
            list = new ThuocBUS().GetAll();
            cb_tenthuoc_dtnew.DataSource = list;
            cb_tenthuoc_dtnew.DisplayMember = "TenThuoc";
            cb_tenthuoc_dtnew.ValueMember = "Mathuoc";
            //txt_tenloaithuoc.Text = cbb_mathuoc.SelectedValue.ToString();

        }
        // thong tin don vi 
        public void ThongTinDonVi()
        {
            List<DonVi> list = new List<DonVi>();
            list = new DonViBUS().GetAll();
            cbb_madonvi.DataSource = list;
            cbb_madonvi.DisplayMember = "TenDonVi";
            cbb_madonvi.ValueMember = "Madonvi";


        }
        // thong tin cach dung
        public void ThongTinCachDung()
        {
            List<CachDung> list = new List<CachDung>();
            list = new CachDungBUS().GetAll();
            cbb_cachdung.DataSource = list;
            cbb_cachdung.DisplayMember = "TenCachDung";
            cbb_cachdung.ValueMember = "MaCachDung";

        }
        public DONTHUOCMOI()
        {
            InitializeComponent();
        }

        private void DONTHUOCMOI_Load(object sender, EventArgs e)
        {
            ThongTinDonVi();
            ThongTinThuoc();
            ThongTinCachDung();
            string ma = Maphieukham;
            string mabenh = Maloaibenh;
            loaddulieu();
            AddChiTietPhieuKham();
        }
        public void loaddulieu()
        {
            dgvcbb_mahtuoc.DataSource = new ThuocBUS().GetAll();
            dgvcbb_mahtuoc.DisplayMember = "TenThuoc";
            dgvcbb_mahtuoc.DataPropertyName = "Maloaithuoc";
            dgvcbb_mahtuoc.ValueMember = "Mathuoc";
            dgvcbb_macachdung.ReadOnly = true;
            dgvcbb_madonvi.ReadOnly = true;
            dgvcbb_mahtuoc.ReadOnly = true;
            dgvcbb_macachdung.DataSource = new CachDungBUS().GetAll();
            dgvcbb_macachdung.DataPropertyName = "Macachdung";
            dgvcbb_macachdung.DisplayMember = "Tencachdung";
            dgvcbb_macachdung.ValueMember = "Macachdung";
            dgvcbb_madonvi.DataSource = new DonViBUS().GetAll();
            dgvcbb_madonvi.DisplayMember = "Tendonvi";
            dgvcbb_madonvi.ValueMember = "Madonvi";
            dgvcbb_madonvi.DataPropertyName = "Madonvi";

        }
        //Them thuoc vao datagridview
        public void AddChiTietPhieuKham()
        {
            BindingList<ChiTietPhieuKham> list = new BindingList<ChiTietPhieuKham>();
            list = listchitietphieukham;
            DGV_DanhSachThuoc.DataSource = list;
            DGV_DanhSachThuoc.Columns["Machitiet"].Visible = false;
            DGV_DanhSachThuoc.Columns["Maphieukham"].Visible = false;
            DGV_DanhSachThuoc.Columns["Thanhtien"].Visible = false;
            DGV_DanhSachThuoc.Columns["Maloaibenh"].Visible = false;
            danhsothutuchodatagridview(DGV_DanhSachThuoc, "STT");

        }

        private void btn_them_dtnew_Click(object sender, EventArgs e)
        {
            try
            {
                int soluong = int.Parse(txt_soluong.Text);
                int soluongton = int.Parse(txt_soluongton.Text);
                if (soluong > soluongton)
                {
                    MessageBox.Show("Dữ liệu nhập chưa đúng");
                    txt_soluong.Text = "";
                    return;
                }
                string maloaithuoc = cb_tenthuoc_dtnew.SelectedValue.ToString();
                string madonvi = cbb_madonvi.SelectedValue.ToString();
                //  int soluong = int.Parse(txt_soluong.Text);
                string macachdung = cbb_cachdung.SelectedValue.ToString();
                int so = listchitietphieukham.Count + new ChiTietPhieuKhamBUS().GetALL().Count + 1;
                string ma = "CT" + so;
                ChiTietPhieuKham dto = new ChiTietPhieuKham(ma, Maphieukham, Maloaibenh, maloaithuoc, madonvi, macachdung, soluong);

                Listchitietphieukham.Add(dto);

                BindingList<ChiTietPhieuKham> temp = new BindingList<ChiTietPhieuKham>();
                DGV_DanhSachThuoc.DataSource = temp;
                temp.Add(dto);

                MessageBox.Show("Thêm thành công!");
                AddChiTietPhieuKham();
                txt_soluong.Text = "";
            }
            catch
            {
                MessageBox.Show("Dữ liệu nhập chưa đúng");
                txt_soluong.Text = "";
            }
        }
        public void danhsothutuchodatagridview(DataGridView table, string colunmName)
        {
            int i = 0;
            while (i < table.RowCount)
            {
                table.Rows[i].Cells[colunmName].Value = i + 1;
                i++;
            }
        }

        private void btn_xoa_dtnew_Click(object sender, EventArgs e)
        {

            if (DGV_DanhSachThuoc.SelectedRows.Count > 0)
            {
                string ma = Convert.ToString(DGV_DanhSachThuoc.SelectedRows[0].Cells[0].Value);
                int so = Convert.ToInt32(ma);
                listchitietphieukham.RemoveAt(so - 1);
                AddChiTietPhieuKham();
            }
        }

        private void cb_tenthuoc_dtnew_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                //txt_tenloaithuoc.Text = cbb_mathuoc.SelectedValue.ToString();
                string mathuoc = cb_tenthuoc_dtnew.SelectedValue.ToString();
                string madonvi = cbb_madonvi.SelectedValue.ToString();
                int soluong = new DonGiaThuocBUS().GetSOLuongTon(mathuoc, madonvi);
                txt_soluongton.Text = soluong.ToString();

            }
            catch
            {
                txt_soluongton.Text = "0";
            }
        }

        private void DGV_DanhSachThuoc_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }
    }
}
